<?php

namespace Beike\Seller\Http\Controllers;

use Beike\Seller\Repositories\OrderRepo;
use Beike\Seller\Repositories\ProductRepo;
use Beike\Seller\Repositories\SellerTransactionRepo;
use Illuminate\Contracts\View\View;

class HomeController extends Controller
{
    /**
     * @return View
     */
    public function index(): View
    {
        $sellerId = current_seller_user()->seller_id;
        $orders   = OrderRepo::getListBuilder()->orderByDesc('created_at')->take(20)->get();
        if (class_basename("\Beike\Shop\Http\Resources\Account\OrderSimpleList")) {
            $orders = \Beike\Shop\Http\Resources\Account\OrderSimpleList::collection($orders);
        } elseif (class_exists("\Beike\Shop\Http\Resources\Account\OrderList")) {
            $orders = \Beike\Shop\Http\Resources\Account\OrderList::collection($orders);
        } else {
            throw new \Exception('Class not exists');
        }
        $data = [
            'product_count' => ProductRepo::count(),
            'order_count'   => OrderRepo::count(),
            'order_total'   => currency_format(OrderRepo::getListBuilder()->sum('total')),
            'balance'       => currency_format(SellerTransactionRepo::getBalance($sellerId)),
            'orders'        => $orders,
            'chart'         => OrderRepo::getOrderChartData($sellerId),
        ];

        return view('seller::home', $data);
    }
}
